package org.jeecg.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.course.entity.GwCourse;
import org.jeecg.modules.course.entity.GwCourseStudent;
import org.jeecg.modules.course.service.IGwCourseService;
import org.jeecg.modules.course.service.IGwCourseStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 课程信息
 * @Author: jeecg-boot
 * @Date:   2024-07-10
 * @Version: V1.0
 */
@Api(tags="课程信息")
@RestController
@RequestMapping("/course/gwCourse")
@Slf4j
public class GwCourseController extends JeecgController<GwCourse, IGwCourseService> {
	@Resource
	private IGwCourseService gwCourseService;

	@Resource
	private IGwCourseStudentService iGwCourseStudentService;
	
	/**
	 * 分页列表查询
	 *
	 * @param gwCourse
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "课程信息-分页列表查询")
	@ApiOperation(value="课程信息-分页列表查询", notes="课程信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GwCourse>> queryPageList(GwCourse gwCourse,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GwCourse> queryWrapper = QueryGenerator.initQueryWrapper(gwCourse, req.getParameterMap());
		Page<GwCourse> page = new Page<GwCourse>(pageNo, pageSize);
		IPage<GwCourse> pageList = gwCourseService.page(page, queryWrapper);

		// 查询用户已经选择的课程标识
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<GwCourseStudent> queryWrapperCourseStudent = new QueryWrapper<>();
		queryWrapperCourseStudent.eq("studentId", sysUser.getId());
		final Set<String> set = iGwCourseStudentService.list(queryWrapperCourseStudent).stream().map(GwCourseStudent::getCourseId).collect(Collectors.toSet());
		pageList.getRecords().forEach(item -> item.setHaveChosen(set.contains(item.getId())));
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param gwCourse
	 * @return
	 */
	@AutoLog(value = "课程信息-添加")
	@ApiOperation(value="课程信息-添加", notes="课程信息-添加")
	//@RequiresPermissions("org.jeecg.modules:gw_course:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GwCourse gwCourse) {
		gwCourseService.save(gwCourse);
		return Result.OK("添加成功！", gwCourse.getId());
	}
	
	/**
	 *  编辑
	 *
	 * @param gwCourse
	 * @return
	 */
	@AutoLog(value = "课程信息-编辑")
	@ApiOperation(value="课程信息-编辑", notes="课程信息-编辑")
	//@RequiresPermissions("org.jeecg.modules:gw_course:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GwCourse gwCourse) {
		gwCourseService.updateById(gwCourse);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "课程信息-通过id删除")
	@ApiOperation(value="课程信息-通过id删除", notes="课程信息-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:gw_course:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		gwCourseService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "课程信息-批量删除")
	@ApiOperation(value="课程信息-批量删除", notes="课程信息-批量删除")
	//@RequiresPermissions("org.jeecg.modules:gw_course:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gwCourseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "课程信息-通过id查询")
	@ApiOperation(value="课程信息-通过id查询", notes="课程信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<GwCourse> queryById(@RequestParam(name="id",required=true) String id) {
		GwCourse gwCourse = gwCourseService.getById(id);
		if(gwCourse==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gwCourse);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gwCourse
    */
    //@RequiresPermissions("org.jeecg.modules:gw_course:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GwCourse gwCourse) {
        return super.exportXls(request, gwCourse, GwCourse.class, "课程信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("gw_course:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GwCourse.class);
    }

	@ApiOperation(value="课程-学习进度")
	@GetMapping(value = "/speed")
	public Result<List<ResultCourse>> speed() {
		List<ResultCourse> list = new ArrayList<>();
		ResultCourse course1 = new ResultCourse();
		course1.setCourse("计算机基础课程一");
		course1.setSpeed("15%");
		list.add(course1);
		ResultCourse course2 = new ResultCourse();
		course2.setCourse("计算机基础课程二");
		course2.setSpeed("45%");
		list.add(course2);
		return Result.OK(list);
	}



	@Data
	private class ResultCourse {

		private String course;

		private String speed;
	}
}
